package com.qing.demo.netty.web;

import com.qing.demo.netty.server.NettyServer;
import io.netty.channel.ChannelFuture;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.ComponentScan;

import java.net.InetSocketAddress;

/**
 * @author caozhipeng
 * @date 2025/7/29
 */
@SpringBootApplication
@ComponentScan("com.qing.demo.netty")
public class NettyApplication implements CommandLineRunner {


    @Value("${netty.host}")
    public String host;
    @Value("${netty.port}")
    public int port;

    @Autowired
    public NettyServer nettyServer;

    public static void main(String[] args) {
        SpringApplication.run(NettyApplication.class, args);
    }


    @Override
    public void run(String... args) throws Exception {
        InetSocketAddress address=new InetSocketAddress(host,port);
        ChannelFuture channelFuture=nettyServer.bing(address);
        Runtime.getRuntime().addShutdownHook(new Thread(()->{
            nettyServer.destroy();
        }));
        channelFuture.channel().closeFuture().syncUninterruptibly();
    }
}
